﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsCatch
    {
        private readonly KeRui.DAL.CmsCatch dal = new KeRui.DAL.CmsCatch();

        public int Add(KeRui.Model.CmsCatch model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatch> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatch> list = new List<KeRui.Model.CmsCatch>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatch item = new KeRui.Model.CmsCatch();
                    if (dt.Rows[i]["CatchID"].ToString() != "")
                    {
                        item.CatchID = int.Parse(dt.Rows[i]["CatchID"].ToString());
                    }
                    item.CatchName = dt.Rows[i]["CatchName"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    item.Url = dt.Rows[i]["Url"].ToString();
                    if (dt.Rows[i]["StartPage"].ToString() != "")
                    {
                        item.StartPage = int.Parse(dt.Rows[i]["StartPage"].ToString());
                    }
                    if (dt.Rows[i]["EndPage"].ToString() != "")
                    {
                        item.EndPage = int.Parse(dt.Rows[i]["EndPage"].ToString());
                    }
                    if (dt.Rows[i]["CurPage"].ToString() != "")
                    {
                        item.CurPage = int.Parse(dt.Rows[i]["CurPage"].ToString());
                    }
                    if (dt.Rows[i]["Interval"].ToString() != "")
                    {
                        item.Interval = int.Parse(dt.Rows[i]["Interval"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    if (dt.Rows[i]["ContentType"].ToString() != "")
                    {
                        item.ContentType = int.Parse(dt.Rows[i]["ContentType"].ToString());
                    }
                    if (dt.Rows[i]["LastTime"].ToString() != "")
                    {
                        item.LastTime = DateTime.Parse(dt.Rows[i]["LastTime"].ToString());
                    }
                    if (dt.Rows[i]["CurTimes"].ToString() != "")
                    {
                        item.CurTimes = int.Parse(dt.Rows[i]["CurTimes"].ToString());
                    }
                    if (dt.Rows[i]["AllTimes"].ToString() != "")
                    {
                        item.AllTimes = int.Parse(dt.Rows[i]["AllTimes"].ToString());
                    }
                    if (dt.Rows[i]["NodeID"].ToString() != "")
                    {
                        item.NodeID = int.Parse(dt.Rows[i]["NodeID"].ToString());
                    }
                    item.NodeName = dt.Rows[i]["NodeName"].ToString();
                    if (dt.Rows[i]["SiteID"].ToString() != "")
                    {
                        item.SiteID = int.Parse(dt.Rows[i]["SiteID"].ToString());
                    }
                    if (dt.Rows[i]["ServerID"].ToString() != "")
                    {
                        item.ServerID = int.Parse(dt.Rows[i]["ServerID"].ToString());
                    }
                    item.Whilelist = dt.Rows[i]["Whilelist"].ToString();
                    item.Blacklist = dt.Rows[i]["Blacklist"].ToString();
                    item.ContentPlus = dt.Rows[i]["ContentPlus"].ToString();
                    if (dt.Rows[i]["SavePic"].ToString() != "")
                    {
                        item.SavePic = int.Parse(dt.Rows[i]["SavePic"].ToString());
                    }
                    if (dt.Rows[i]["ParseID"].ToString() != "")
                    {
                        item.ParseID = int.Parse(dt.Rows[i]["ParseID"].ToString());
                    }
                    item.ParseName = dt.Rows[i]["ParseName"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int CatchID)
        {
            return this.dal.Delete(CatchID);
        }

        public List<KeRui.Model.CmsCatch> GetList(int ServerID)
        {
            string strWhere = "";
            if (ServerID > 0)
            {
                strWhere = "ServerID=" + ServerID;
            }
            string strOrder = "ParseID asc,catchid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatch", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatch> GetList(int ServerID, int SiteID)
        {
            string strWhere = "";
            if (ServerID > 0)
            {
                strWhere = "ServerID=" + ServerID;
            }
            if (SiteID > 0)
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = "SiteID=" + SiteID;
            }
            string strOrder = "catchid asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatch", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatch> GetListByCache(int ServerID)
        {
            string cacheKey = "CmsCatchList-" + ServerID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(ServerID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsCatch>) cache;
        }

        public KeRui.Model.CmsCatch GetModel(int CatchID)
        {
            return this.dal.GetModel(CatchID);
        }

        public KeRui.Model.CmsCatch GetModelByCache(int CatchID)
        {
            string cacheKey = "CmsCatchModel-" + CatchID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(CatchID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatch) cache;
        }

        public DataSet GetSelect()
        {
            string strWhere = "";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsCatch", strWhere, "CatchID Desc", "Url,CatchName");
        }

        public DataSet GetSelect(int ParseID)
        {
            string strWhere = "ParseID=" + ParseID;
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsCatch", strWhere, "CatchID Desc", "Url,CatchName");
        }

        public int Update(KeRui.Model.CmsCatch model)
        {
            return this.dal.Update(model);
        }
    }
}

